<template>
  <div class="big_box">
    <div class="login_box">
      <div class="logo_box">
        <img src="../assets/logo.png" alt="" />
      </div>

      <el-form
        ref="login_form"
        :rules="login_form_rules"
        :model="form_data"
        label-width="0px"
        class="el_form"
      >
        <el-form-item prop="user_name" label="">
          <el-input
            v-model="form_data.user_name"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password" label="">
          <el-input
            type="password"
            v-model="form_data.password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button @click="fun_login" type="primary" plain>登录</el-button>
          <el-button @click="rst_fun" type="primary" plain>重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "MyLogin",
  data() {
    return {
      form_data: {
        user_name: "admin",
        password: "123456",
      },
      login_form_rules: {
        user_name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 9, message: "长度在3-9位", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 4, max: 16, message: "长度在4-16位", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    fun_login() {
      this.$refs.login_form.validate(async (value) => {
        if (!value) return alert("格式错误");
        // 预验证结束 开始验证
        const res_data = await this.$axios.post("/login", {
          username: this.form_data.user_name,
          password: this.form_data.password,
        });
        if (res_data.data.meta.status == 200) {
          window.sessionStorage.setItem("token", res_data.data.data.token);
          this.$router.push("/home");
          this.$Message.success("登录成功");
        } else {
          this.$Message.error("登录失败!");
        }
      });
    },
    // 重置表单
    rst_fun() {
      this.$refs.login_form.resetFields();
    },
  },
};
</script>

<style lang="css" scoped>
.big_box {
  position: relative;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 900px;
  background-color: #264a6b;
}
.big_box > div {
  width: 600px;
  height: 400px;
  background-color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.big_box > div .logo_box {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translate(-50%, -70%);
  width: 200px;
  height: 200px;
  border: 10px solid #fff;
  background-color: #fff;
  border-radius: 50%;
  padding: 5px;
  box-shadow: 0px 0px 10px 1px rgba(0 253 0);
  z-index: 999;
}
.big_box > div .logo_box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
}
.big_box > div .el_form {
  /* margin-top: 50px; */
  position: absolute;
  width: 100%;
  top: 50%;
  transform: translateY(-50%);
  padding: 0 30px;
  box-sizing: border-box;
}
.btn {
  display: flex;
  justify-content: flex-end;
}
</style>